package com.jiezuo.flowable.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiezuo.auth.entity.User;
import com.jiezuo.common.base.BaseController;
import com.jiezuo.common.util.ResultUtil;
import com.jiezuo.common.vo.PageVo;
import com.jiezuo.common.vo.Result;
import com.jiezuo.flowable.entity.ProcessInstanceAddQuery;
import com.jiezuo.flowable.entity.ProcessInstancePageQuery;
import com.jiezuo.flowable.entity.ProcessInstanceStateQuery;
import com.jiezuo.flowable.service.UserProcessInstanceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @description 流程实例
 * @author 庞留杰
 * @date 2020/9/14 9:38
 * @version V1.0
 **/
@Slf4j
@Transactional
@RestController
@RequestMapping("/processInstance")
public class UserProcessInstanceController extends BaseController {

    @Autowired
    private UserProcessInstanceService service;

    /**
     * 功能描述: 启动：提交一个申请单
     *   启动流程实例(提交一个申请单)，实例启动成功，返回当前活动任务
     * @author: 庞留杰
     * @exception
     * @date: 2020/9/11 9:17
     */
    @PostMapping("/startByKey")
    public Result startByKey(@RequestBody ProcessInstanceAddQuery addQuery) {
        User user = this.getLoginUser();
        String processInstanceId = service.startByKey(user, addQuery);
        //通过processInstance.getId() 可以判断流程是否发起成功
        return new ResultUtil<>().setData(processInstanceId);
    }

    /**
     * 功能描述: 分页查询流程实例【操作表：act_ru_execution】
     * @author: 庞留杰
     * @param processInstancePageQuery
     * @return:
     * @exception
     * @date: 2020/9/14 9:44
     */
    @GetMapping("/findProcessInstancePage")
    public Result findProcessInstancePage(@ModelAttribute PageVo pageVo, @ModelAttribute ProcessInstancePageQuery processInstancePageQuery) {
        Page page = service.findProcessInstancePage(pageVo, processInstancePageQuery);
        return new ResultUtil<>().setData(page);
    }

    /**
     * 功能描述: 流程的挂起、激活
     * @author: 庞留杰
     * @param processInstanceStateQuery
     * @return: Result
     * @exception
     * @date: 2020/9/14 9:53
     */
    @PostMapping("/editProcessInstanceState")
    public Result editProcessInstanceState(@RequestBody ProcessInstanceStateQuery processInstanceStateQuery) {
        String resutl = service.editProcessInstanceState(processInstanceStateQuery);
        return new ResultUtil<>().setSuccessMsg(resutl);
    }

    //删除", notes = "删除流程", produces = "application/json")
    @PostMapping (value = "/deleteByProcessInstance/{processInstanceId}")
    public Result deleteByProcessInstance(@PathVariable String processInstanceId) {
        service.deleteByProcessInstance(processInstanceId);
        return new ResultUtil<>().setSuccessMsg("删除成功");
    }

}
